package yamen.bdwm.datastruct;

import yamen.bdwm.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class MoreAdapter extends WmDataAdapter {

	onMoreClicked mOnMore;
	boolean showmore=true;
	
	public void ShowMore()
	{
		showmore=true;
		this.notifyDataSetChanged();
	}
	public void HideMore()
	{
		showmore=false;
		this.notifyDataSetChanged();
	}
	
	public interface onMoreClicked
	{
		void Load(onMoreOver l);
		boolean ShowMore();
	}
	
	public interface onMoreOver
	{
		void OnMoreOver();
	}
	
	public MoreAdapter(int resouce, WmList l,onMoreClicked onmoreClicked) {
		super(resouce, l);
		this.mOnMore=onmoreClicked;
	}

	public int getCount() {
		if(list.size()==0)return 0;
		return list.size() + (showmore?1:0);
	}

	public Object getItem(int position) {
		if(position < list.size())
			return list.get(position);
		else
			return null;
	}

	public long getItemId(int position) {
//		if(position < list.size())
//			return Long.parseLong(list.get(position).id);
//		else
			return 0;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

	public void SelectAndMark(int a) {
		if (a != list.size()) {
			setSelectItem(a);
			this.notifyDataSetChanged();
		}
	}

	protected int selectItem = -1;

	public View getView(int position, View convertView, ViewGroup parent) {
		if(position==list.size())
		{
			final View view=mLayoutInflater.inflate(R.layout.more, null);
			view.findViewById(R.id.more).setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					view.findViewById(R.id.item_waiting).setVisibility(View.VISIBLE);
					view.findViewById(R.id.item_more_tv).setVisibility(View.GONE);
					mOnMore.Load(new onMoreOver() {
						
						
						public void OnMoreOver() {
							view.findViewById(R.id.item_waiting).setVisibility(View.GONE);
							view.findViewById(R.id.item_more_tv).setVisibility(View.VISIBLE);
						}
						
					});
					showmore = mOnMore.ShowMore();
				}
			});
			return view;
		}
		return ((WmItem) getItem(position)).getView(null, mLayoutInflater,
				mResource, convertView, parent);
	}

}
